package com.flife;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint;
import com.facebook.react.defaults.DefaultReactActivityDelegate;

public class MainActivity extends ReactActivity {
    private static final int REQUEST_CODE_ALL_PERMISSIONS = 100;

    private final String[] PERMISSIONS = {
            Manifest.permission.SET_ALARM,
            Manifest.permission.VIBRATE,
            Manifest.permission.INTERNET
    };

    @Override
    protected String getMainComponentName() {
        return "flife";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("MainActivity", "onCreate");
        super.onCreate(savedInstanceState);
        checkAndRequestPermissions();
    }

    @Override
    protected ReactActivityDelegate createReactActivityDelegate() {
        return new DefaultReactActivityDelegate(
                this,
                getMainComponentName(),
                DefaultNewArchitectureEntryPoint.getFabricEnabled());
    }

    private void checkAndRequestPermissions() {
        Log.d("MainActivity", "checkAndRequestPermissions");
        // 检查应用程序是否已被授予所有所需的权限
        boolean allPermissionsGranted = true;
        for (String permission : PERMISSIONS) {
            if (ContextCompat.checkSelfPermission(this, permission)
                    != PackageManager.PERMISSION_GRANTED) {
                allPermissionsGranted = false;
                break;
            }
        }
        Log.d("MainActivity", "checkAndRequestPermissions:" + allPermissionsGranted);
        // 如果没有权限，则请求所有权限
        if (!allPermissionsGranted) {
            Log.d("MainActivity", "申请权限");
            ActivityCompat.requestPermissions(this, PERMISSIONS, REQUEST_CODE_ALL_PERMISSIONS);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
      Log.d("MainActivity", "requestCode：" + requestCode);  
      Log.d("MainActivity", "grantResults" + grantResults); 
      if (requestCode == REQUEST_CODE_ALL_PERMISSIONS) {
            boolean allPermissionsGranted = true;
            for (int grantResult : grantResults) {
                if (grantResult != PackageManager.PERMISSION_GRANTED) {
                    allPermissionsGranted = false;
                    break;
                }
            }

            if (allPermissionsGranted) {
                // 如果用户授予了所有权限，则执行应用程序的主要逻辑
                // 在这里执行你的应用程序主要逻辑
                Log.d("MainActivity", "authed ok");
            } else {
                // 如果用户拒绝了任何一个权限，可以给出相应的提示或处理
                Log.d("MainActivity", "authed fail");
            }
        }
    }
}
